package rs;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.nutz.json.Json;

import rs.util.Log;

public class ModelSeller implements Runnable{
	
	public boolean shouldStop = false;
	
	
	public void work(){
		try{
			//Map<String, Object> model = ModelStore.store.take();
			List<Map<String, Object>> models = new LinkedList<Map<String,Object>>();
			ModelStore.store.drainTo(models);
			
			List<Map<String, Object>> toSend = new LinkedList<Map<String,Object>>();
			//过滤 .
			//score >= 1000 才投递
			for(Map<String, Object> model: models){
				if(Integer.parseInt(model.get("score").toString()) >= 1000){
					toSend.add(model);
				}
			}
			//过滤结束.
			
			
			Jsoup.connect(Conf.REMOTE_ADDRESS)
				.data("json", Json.toJson(toSend))
				.data("pass", "p")
				.post();
			Log.info("Baos TO SEND: " + toSend.size() +  "\n" + Json.toJson(toSend));
		} catch(Exception e){
			// seller 就是没命的干活,有问题歇一秒继续.
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			work();
		}
	}

	@Override
	public void run() {
		while (!shouldStop) {
			work();
			try {
				Thread.sleep(1000 * 2);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
